package array;

import java.util.BitSet;
import java.util.function.IntBinaryOperator;

/**
 * 位图
 */
public class BitMap {
    private int size;
    private int[] m;
    private int mLen;

    public BitMap(int size) {
        this.size = size;
        mLen = size / 32 + (size % 32 == 0 ? 0 : 1);
        m = new int[mLen];
    }

    public void setBit(int v) {
        if (v >= size) throw new RuntimeException("data bound not support.");

        int index = v / 32;
        int bitIndex = v % 32;

        m[index] = m[index] | (1 << (31 - bitIndex));
    }

    public boolean contains(int v) {
        int index = v / 32;
        int bitIndex = v % 32;
        int r = m[index] & (1 << (31 - bitIndex));
        return r != 0;
    }

    public static void main(String[] args) {
        BitMap bitMap = new BitMap(20);
        for (int i = 0; i < 15; i++) {
            bitMap.setBit(i);
        }
        int i = bitMap.m[0];
        System.out.println("Integer.toBinaryString(i) = " + Integer.toBinaryString(i));
        
        System.out.println("bitMap = " + bitMap);
    }

}
